<template>
  <div class="v-order-list">
    <nav>
      <div class="nav-item"><a href="javascript:;" :class="{active: status == 0}" @click="checkOrderList(0)">全部</a>
      </div>
      <div class="nav-item"><a href="javascript:;" :class="{active: status == 100}" @click="checkOrderList(100)">待发货</a>
      </div>
      <div class="nav-item"><a href="javascript:;" :class="{active: status == 200}" @click="checkOrderList(200)">待收货</a>
      </div>
      <div class="nav-item"><a href="javascript:;" :class="{active: status == 1000}"
                               @click="checkOrderList(1000)">已完成</a>
      </div>
    </nav>
    <ul class="content" v-infinite-scroll="getOrderList" infinite-scroll-disabled="nonext" infinite-scroll-distance="10"
        infinite-scroll-immediate-check="false">
      <li class="list" v-for="(item, index) in orders" :key="index" :id="item.id">
        <div class="top">
          <div class="time">{{item.created_at}}</div>
          <div class="t_status" :class="{high: item.status == 200}">{{setStatus(item.status)}}</div>
        </div>
        <main>
          <div class="main-item" :class="{last: i === item.wares.length - 1}" v-for="(good, i) in item.wares" :key="i" :id="good.ware_id">
            <img :src="good.image">
            <div class="right">
              <p>{{good.name}}</p>
              <p class="total">共{{good.num}}件商品</p>
            </div>
          </div>
        </main>
        <div class="down">
          <router-link class="btn" :to="'/delivery?id=' + item.id" v-if="item.status==200">查看物流</router-link>
          <router-link class="btn" :to="'/order-detail?id=' + item.id">订单详情</router-link>
          <div class="btn" @click="delOrder(item.id,index)" v-if="item.status==1000">删除订单</div>
          <div class="btn comfirm" @click="comfirmOrder(item.id,index)" v-if="item.status==200">确认收货</div>
        </div>
      </li>
      <li class="noaddress" v-if="orders.length==0">
        <p style="font-size:0.3rem;color:#999;margin-bottom:0.2rem;">你的订单还是空的</p>
      </li>
    </ul>
    <div class="order_hint_box">
      <div class="order_hint_content">
        <div class="order_hint_background">
          <div class="order_hint_tit"></div>
        </div>
      </div>
    </div>
    <!--<div class="loadmore" v-if="page < total" @click="loadMore">加载更多</div>-->
  </div>
</template>
<script>
  import {Indicator} from 'mint-ui';
  import api from '@/assets/js/api'
  import Wx from '@/assets/js/weixin';
  import {Toast} from 'mint-ui';
  import {InfiniteScroll} from 'mint-ui';
  import store from 'store';

  export default {
    data() {
      return {
        status: 0,
        orders: [],
        page: 1,
        total: 0,
        nonext: false,
        token:this.$route.query.token,
        headerData:{}
      };
    },
    created() {
      Wx.hide();
      this.getOrderList(0);
    },
    components: {
      'Indicator': Indicator,
    },
    methods: {
      getOrderList(status) {
        Indicator.open('加载中...');
        if (this.page == 1) {
          this.orders = [];
          this.nonext = false;
        }
        var smData = {
          // status: this.status,
          page: this.page,
        };
        if (status) smData.status = status;
        api.fetch('/api/orders',{params:smData}).then((res)=>{
          if (res.meta.pagination.count > 0) {
            if (res.meta.pagination.per_page > res.meta.pagination.count) {
              this.nonext = true;
            }
            if (this.orders.length == 0) {
              this.orders = res.data;
              this.page = this.page + 1;
            } else {
              this.orders = this.list.concat(res.data);
              this.page = this.page + 1;
            }
          }
          Indicator.close();
        }).catch();
      },
      checkOrderList(status) {
        this.status = status;
        this.page = 1;
        this.getOrderList(status);
      },
      setStatus(status) {
        let statusText = "";
        switch (status+'') {
          case "100":
            statusText = "待发货";
            break;
          case "200":
            statusText = "待收货";
            break;
          case "1000":
            statusText = "已完成";
        }
        return statusText;
      },
      comfirmOrder(id, index) {
        api.fetch('/api/orders/'+id+'/confirm', {
          method: 'post',
          data: {
            _method: 'put',
          }
        }).then((res) => {
          Toast({
            message: '收货完成！',
            iconClass: 'mint-toast-icon mintui mintui-success'
          });
          if (this.status == 100) {
            this.orders.splice(index,1);
          } else {
            this.orders[index].status = 1000;
          }
        }).catch();
      },
      delOrder(id, index) {
        api.fetch('/api/orders/' + id, {
          method: 'post',
          data: {
            _method: 'delete',
          }
        }).then((res) => {
          Toast({
            message: '删除成功！',
            iconClass: 'mint-toast-icon mintui mintui-success'
          });
          this.orders.splice(index,1);
        }).catch();
      }
    }
  };

</script>
<style scoped lang="less">
  .v-order-list {
    background-color: #efefef;
    box-sizing: border-box;
    min-height: 100%;
    .noaddress {
      background: url("../../assets/images/plane.png") no-repeat 0.94rem 2.68rem;
      background-size: 4.99rem 2.67rem;
      text-align: center;
      padding-top: 7rem;
    }
    nav {
      height: 0.84rem;
      display: flex;
      .nav-item {
        font-size: 0.28rem;
        color: #686868;
        a {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100%;
        }
        a.active {
          color: #ffb52b;
        }
      }
    }
    .content {
      padding-top: 0.3rem;
      .list {
        background: #fff;
        padding: 0 0.3rem 0;
        margin-bottom: 0.3rem;
        .top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 0.8rem;
          font-size: 0.24rem;
          color: #999;
          .time {
            font-size: 0.28rem;
            color: #686868;
          }
          .t_status {
            font-size: 0.28rem;
            color: #a5a5a5;
            &.high {
              color: #ffb52b;
            }
          }
        }
        main {
          box-sizing: border-box;
          padding: 0.1rem 0;
          border-top: 0.01rem solid #e1e4eb;
          border-bottom: 0.01rem solid #e1e4eb;
          .main-item {
            height: 1.7rem;
            padding-top: 0.1rem;
            display: flex;
            justify-content: flex-start;
            &.last .right {
              border: 0;
            }
          }
          img {
            width: 1.5rem;
            height: 1.5rem;
            margin-right: 0.3rem;
            border-radius: 0.1rem;
          }
          .right {
            width: 5.26rem;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            height: 100%;
            padding: 0.2rem 0rem;
            border-bottom: 0.01rem solid #e1e4eb;
            p {
              height: 0.4rem;
              font-size: 0.28rem;
              color: #333;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            p.total {
              font-size: 0.24rem;
              color: #999;
            }
          }
        }
        .down {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          height: 0.94rem;
          .btn {
            width: 1.5rem;
            height: 0.54rem;
            border-radius: 0.1rem;
            color: #999;
            font-size: 0.26rem;
            border: 0.01rem solid #999;
            text-align: center;
            line-height: 0.54rem;
            margin-right: 0.2rem;
            &.comfirm {
              background: #ffb52b;
              border: 0.01rem solid #ffb52b;
              color: #fff;
            }
          }
        }
      }
    }

    .order_hint_box {
      position: fixed;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background-color: rgba(0, 0, 0, 0.5);
      display: none;
      z-index: 9998;
      transition: all 0.3s ease;
    }

    .order_hint_content {
      display: table-cell;
      vertical-align: middle;
    }

    .order_hint_content .order_hint_background {
      margin: 0 auto;
      width: 60%;
      background-color: #2d2c2c;
      border-radius: 0.06rem;
      overflow: hidden;
      color: #fff;
      z-index: 100;
    }

    .order_hint_content .order_hint_background .order_hint_tit {
      font-size: 0.3rem;
      padding: 0.4rem 0;
      text-align: center;
    }

    .loadmore {
      height: 0.8rem;
      text-align: center;
      line-height: 0.8rem;
    }
  }
</style>
